﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using Ensemble_Windows.UI.Core;

namespace Ensemble_Windows.UI.ProgressBar
{
    /// <summary>
    ///     ProgressBar类
    /// </summary>
    public sealed class ProgressBar : Control
    {
        private double _maximum = 100;

        private Color _progressColor = Helpers.BaseColor;
        private double _value;

        /// <summary>
        ///     进度条
        /// </summary>
        public ProgressBar()
        {
            SetStyle(
                ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            BackColor = Color.FromArgb(60, 70, 73);
            Height = 42;
        }

        /// <summary>
        ///     最大值
        /// </summary>
        public double Maximum
        {
            get { return _maximum; }
            set
            {
                if (value < _value)
                    _value = value;
                _maximum = value;
                Invalidate();
            }
        }

        /// <summary>
        ///     值
        /// </summary>
        public double Value
        {
            get { return _value; }
            set
            {
                if (value > _maximum)
                {
                    value = _maximum;
                    Invalidate();
                }

                _value = value;
                Invalidate();
            }
        }

        /// <summary>
        /// </summary>
        public Color ProgressColor
        {
            get { return _progressColor; }
            set { _progressColor = value; }
        }

        /// <summary>
        ///     缩放
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 42;
        }

        /// <summary>
        ///     创建句柄
        /// </summary>
        protected override void CreateHandle()
        {
            base.CreateHandle();
            Height = 42;
        }

        /// <summary>
        ///     增加
        /// </summary>
        /// <param name="amount">数量</param>
        public void Increment(int amount)
        {
            Value += amount;
        }

        /// <summary>
        ///     重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            UpdateColors();

            var b = new System.Drawing.Bitmap(Width, Height);
            var g = Graphics.FromImage(b);

            var with15 = g;
            with15.SmoothingMode = SmoothingMode.HighQuality;
            with15.PixelOffsetMode = PixelOffsetMode.HighQuality;
            with15.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            with15.Clear(BackColor);

            //-- Progress Value
            //int iValue = Convert.ToInt32(((float)_Value) / ((float)(_Maximum * Width)));


            base.OnPaint(e);
            g.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(b, 0, 0);
            b.Dispose();
        }

        private void UpdateColors()
        {
            var colors = Helpers.GetColors(this);

            _progressColor = colors.Base;
        }
    }
}